﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class BomDetailBLL : IBaseInfoDetail
    {
        private static readonly IBomDetailDA bomDetailDA = DataAccess.CreateBomDetailDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly ISkuDA SkuDA = DataAccess.CreateSkuDA();

        public BomDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public BomDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public BomDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> mainID, List<string> subId, List<string> pk3)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                if (mainID.Count != subId.Count)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2270_007", this.pubParasInfo.UserLanguage);
                }
                List<BomDetailInfo> bomDetailInfos = new List<BomDetailInfo>();
                BomDetailInfo bomDetailInfo = null;
                for (i = 0; i < mainID.Count; i++)
                {
                    bomDetailInfo = bomDetailDA.GetBomDetailByID(dataBase, tran, mainID[i], subId[i], pk3[i], this.dataBaseID[0]);
                    bomDetailInfos.Add(bomDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (bomDetailDA.DeleteBomDetail(dataBase, tran, mainID, subId, pk3, this.dataBaseID) > 0)
                {
                    for (i = 0; i < mainID.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<BomDetailInfo>(dataBase, tran, bomDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "BOM_DETAIL", this.dataBaseID[0]);
                        string CS00002 = temp;
                        temp = CS00002 + mainID[i] + " , " + subId[i] + " , " + pk3[i] + ";";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllDetailInfo()
        {
            return bomDetailDA.GetAllBomDetail(this.dataBaseID[0]);
        }

        public DataSet GetDetaiInfolByQueryEntity(object obj)
        {
            BomDetailQueryEntity bomDetailQuery = obj as BomDetailQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(bomDetailQuery.OwnerID))
            {
                sqlWhere.Add(" OWNER_ID " + bomDetailQuery.OwnerID);
            }
            if (!string.IsNullOrEmpty(bomDetailQuery.SkuID))
            {
                sqlWhere.Add(" SKU_ID " + bomDetailQuery.SkuID);
            }
            if (!string.IsNullOrEmpty(bomDetailQuery.ComponentSkuID))
            {
                sqlWhere.Add(" COMPONENT_SKU_ID " + bomDetailQuery.ComponentSkuID);
            }
            if (!string.IsNullOrEmpty(bomDetailQuery.SeqNo))
            {
                sqlWhere.Add(" SEQ_NO " + bomDetailQuery.SeqNo);
            }
            if (!string.IsNullOrEmpty(bomDetailQuery.Qty))
            {
                sqlWhere.Add(" QTY " + bomDetailQuery.Qty);
            }
            return bomDetailDA.GetBomDetailByQueryList(sqlWhere, bomDetailQuery, this.dataBaseID[0]);
        }

        public object GetDetailInfoByID(string mainID, string subID, string pk3)
        {
            return bomDetailDA.GetBomDetailListByID(mainID, subID, this.dataBaseID[0]);
        }

        public List<object> GetDetailInfoListByID(string mainID, string subID)
        {
            List<BomDetailInfo> bomDetails = bomDetailDA.GetBomDetailListByID(mainID, subID, this.dataBaseID[0]);
            List<object> objs = null;
            if ((bomDetails != null) && (bomDetails.Count > 0))
            {
                objs = new List<object>();
                for (int i = 0; i < bomDetails.Count; i++)
                {
                    objs.Add(bomDetails[i]);
                }
            }
            return objs;
        }

        public string InsertDetail(object obj)
        {
            BomDetailInfo bomDetailInfo = obj as BomDetailInfo;
            string bomDetailID = string.Empty;
            try
            {
                if (SkuDA.GetSkuByID(bomDetailInfo.OwnerID, bomDetailInfo.ComponentSkuID, this.dataBaseID[0]) == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2270_005", this.pubParasInfo.UserLanguage, new List<string> { bomDetailInfo.ComponentSkuID });
                }
                if (bomDetailDA.CheckBomDetailIDUnique(bomDetailInfo.OwnerID, bomDetailInfo.SkuID, bomDetailInfo.ComponentSkuID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2270_006", this.pubParasInfo.UserLanguage, new List<string> { bomDetailInfo.ComponentSkuID, bomDetailInfo.OwnerID, bomDetailInfo.SkuID });
                }
                if (bomDetailInfo == null)
                {
                    return bomDetailID;
                }
                bomDetailInfo.CreatedBy = this.pubParasInfo.UserID;
                bomDetailInfo.UpdatedBy = this.pubParasInfo.UserID;
                bomDetailInfo.CreatedDate = DateTime.Now;
                bomDetailInfo.UpdatedDate = DateTime.Now;
                if (bomDetailDA.InsertBomDetail(bomDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<BomDetailInfo>(null, bomDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "BOM_DETAIL", this.dataBaseID[0]);
                    bomDetailID = bomDetailInfo.ComponentSkuID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return bomDetailID;
        }

        public string UpdateDetail(object obj)
        {
            string bomDetailID = string.Empty;
            try
            {
                BomDetailInfo bomDetail = obj as BomDetailInfo;
                bomDetail.UpdatedBy = this.pubParasInfo.UserID;
                bomDetail.UpdatedDate = DateTime.Now;
                BomDetailInfo oldBomDetailInfo = bomDetailDA.GetBomDetailByID(bomDetail.OwnerID, bomDetail.SkuID, bomDetail.ComponentSkuID, this.dataBaseID[0]);
                if (bomDetailDA.UpdateBomDetail(bomDetail, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<BomDetailInfo>(oldBomDetailInfo, bomDetail, AuditAction.Update, this.pubParasInfo.UserID, "BOM_DETAIL", this.dataBaseID[0]);
                    bomDetailID = bomDetail.OwnerID + " , " + bomDetail.SkuID + " , " + bomDetail.ComponentSkuID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return bomDetailID;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

